package com.tom;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class SqlJdbc {

	public static void main(String[] args) {
		Connection conn;
		Statement stmt;
		PreparedStatement prestmt;
		ResultSet rs1,rs2;
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;database=CH06範例資料庫;user=sa;password=111");
			//使用SQL語法
			stmt = conn.createStatement();
			
			rs1 = stmt.executeQuery("select * from 員工");
			while (rs1.next()){
				System.out.println(rs1.getString("姓名"));
			}
				System.out.println("=======================");	
			//使用預存程序	
			prestmt = conn.prepareStatement("{call spQueryProd (default)}");
			rs2 = prestmt.executeQuery();
			while (rs2.next()){
				System.out.println(rs2.getString("產品名稱"));
				System.out.println(rs2.getString("備註"));
			}
			rs1.close();
			rs2.close();
			stmt.close();
			conn.close();

			
			} catch (Exception e) {
			
			System.out.println(e.getMessage());

		}
	}
}

